<!DOCTYPE html>
<html>

<body>
    <div style="display: none;">
        <button onclick="sendMessage()" type="button">23</button>
    </div>

    <div class="ccomform"
        style="inset: 0px;background-color: rgba(0,0,0,0.65);position: fixed;z-index: 12;display: none;"></div>
    <div class="ccomform" style="position: fixed;inset: 0px;z-index: 13;display: none;">
        <div
            style="  border-radius: 5px; border: 1px solid #ccc;  padding: 20px;  margin: 0px auto;margin-top: 20vh;height: 130px;background-color: #f5f5f5;width: 200px;display: flex;justify-content: space-around;align-items: center;flex-direction: column;">
            <div id="messageInfo" style="padding: 12px;color: black;font-weight: 600;">确认删除？</div>
            <div class="f">
                <div class="f">
                    <button class="button" style="margin-right: 20px;" onclick="queren(true)">确认</button>
                    <button class="button3" onclick="queren(false)">取消</button>
                </div>
            </div>
        </div>
    </div>
    <div style="width:calc(100% - 48px);height:100vh;padding:24px">
        <!-- <div class="f" style="flex-direction: row;justify-content: left;">
            <button onclick="showEdit(true)" type="button" class="button_t" style="margin-bottom: 8px; margin-right: 8px;">新增</button>
            <button onclick="refresh()" type="button" class="button_t" style="margin-bottom: 8px;">刷新</button>
        </div> -->
        <table id="myTable">
            <tr>
                <th style="width: 200px;">ID</th>
                <th style="width: calc(100% - 300px);">内容</th>
                <th style="width: 100px;">操作</th>
            </tr>
        </table>
    </div>
</body>
<script>
    const vscode = acquireVsCodeApi()
    function sendMessage() {
        const message = {
            command: 'showMessage',
            text: 'Hello from HTML!'
        };
        vscode.postMessage(message);
    }
    var itemInfo = {}
    window.addEventListener('message', event => {
        const message = event.data;
        console.log('mmm', message)
        // 处理来自插件的消息
        if (message.command == 'globalStateInit') {
            loadInfo(message.da)
        } else if (message.command == 'globalStateRefresh') {
            refresh()
        }
    });

    // 在 HTML 加载完成后通知插件进行初始化
    window.addEventListener('DOMContentLoaded', () => {
        vscode.postMessage({ command: 'globalStateInit' });
    });
    var ccomform_callback = null
    function ccomform(open, callback) {
        let dd = 'none';
        if (open) {
            dd = 'block';
        }
        let cc = document.getElementsByClassName('ccomform');
        cc[0].style.display = dd;
        cc[1].style.display = dd;
        ccomform_callback = callback
    }
    function queren(st) {
        if (st && ccomform_callback) {
            try {
                ccomform_callback()
            } catch (err) {
                console.log(err)
            }
        }
        ccomform(false)
    }

    // 一下是页面逻辑
    var table = document.getElementById("myTable");

    function addda2Table(key, value) {
        var newRow = table.insertRow(-1); // 在表格的最后插入新行  
        var newCell1 = newRow.insertCell(0);
        var newCell2 = newRow.insertCell(1);
        var newCell3 = newRow.insertCell(2);

        newCell1.innerHTML = key + '';
        newCell1.style.width = '200px';
        newCell2.innerHTML = value + '';
        newCell2.style.width='calc(100% - 300px)';

        newCell3.innerHTML = "<div class='f ' style='flex-direction: row;'><button class='button_t' onclick='del(\"" + key + "\")'>删除</button></div>";
        newCell3.style.width = '100px';
    }
    function del(id) {
        ccomform(true, () => {
            vscode.postMessage({ command: 'globalStateDel', da: { id: id } });
        })
    }
    function showEdit(canedit) {

    }
    function refresh() {
        table.innerHTML = '<tr><th style="width: 200px;">ID</th><th>内容</th><th style="width: 100px;">操作</th></tr>'
        vscode.postMessage({ command: 'globalStateInit' });
    }
    function loadInfo(da) {
        console.log(da)
        for (var i in da) {
            addda2Table(i, da[i])
        }


    }
</script>
<style>
    table {
        border-collapse: collapse;
        /* 边框合并为一个 */
        box-sizing: border-box;
        width: 100%;
        /* 减去左右的内边距 */
        /*列宽由表格宽度和列宽度设定*/
        table-layout: fixed;
    }

    th,
    td {
        border: 1px solid #aaaaaa;
        /* 单元格边框 */
        padding: 8px;
        /* 内边距 */
        text-align: left;
        /* 文本左对齐 */
        word-break: break-word;
    }

    th {
        font-weight: 500;
    }

    body {
        font-family: Arial, sans-serif;
    }

    .f {
        display: flex;
        justify-content: center;
        align-items: center;
    }

    a {
        cursor: pointer;
    }


    .button {
        display: inline-block;
        padding: 4px 20px;
        background-color: #007BFF;
        color: #fff;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        text-align: center;
        font-size: 16px;
    }

    .button_t {
        display: inline-block;
        padding: 4px 4px;
        background-color: transparent;
        border: 1px solid var(--vscode-editor-foreground);
        border-radius: 5px;
        cursor: pointer;
        text-align: center;
        font-size: 14px;
        color: var(--vscode-editor-foreground);
        margin: 4px;
    }

    .button2 {
        display: inline-block;
        padding: 4px 20px;
        background-color: #fafafa;
        color: red;
        border: 1px solid red;
        border-radius: 5px;
        cursor: pointer;
        text-align: center;
        font-size: 16px;
    }

    .button3 {
        display: inline-block;
        padding: 4px 20px;
        background-color: #fafafa;
        color: #0056b3;
        border: 1px solid #0056b3;
        border-radius: 5px;
        cursor: pointer;
        text-align: center;
        font-size: 16px;
    }

    .button:hover {
        background-color: #0056b3;
    }

    .button3:hover {
        background-color: #93c8f0;
    }

    html,
    body {
        width: 100%;
        height: 100%;
        margin: 0px;
        padding: 0px;
    }
</style>

</html>